package com.etooling.framework.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etooling.framework.mapper.EmployeeMapper;
import com.etooling.framework.svo.ReplyObj;
import com.etooling.framework.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")

    @ResponseBody
    ReplyObj home(HttpServletRequest request) {
        return WebUtils.flushSuccess("欢迎使用易图灵!");
    }

    @Autowired
    private EmployeeMapper employeeMapper;

    @RequestMapping("/testExcel")
    @ResponseBody
    public void exportExcel(HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment; filename=exported_excel.xls");
            PrintWriter out = response.getWriter();
            // HTML内容
            String htmlContent = "<html><body><table><tr><td>This is</td><td>HTML</td></tr></table></body></html>";
            // 输出HTML内容到响应
            out.println(htmlContent);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * @param request
     * @param name
     * @return
     */
    @RequestMapping("/testMyBaits")
    @ResponseBody
    ReplyObj testMyBaits(HttpServletRequest request,
                         @RequestParam(defaultValue = "", required = false, value = "name") String name) {
        QueryWrapper<Map<String, Object>> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("mobile", "13");
        queryWrapper.eq("company_id", "root");
        queryWrapper.and(wrapper -> {
            wrapper.like("user_name", name)
                    .or()
                    .like("user_admin_name", name);
        });
        queryWrapper.orderByAsc("mobile");
        Page<Map<String, Object>> page = new Page<>(1, 5);
        // 执行查询
        IPage<Map<String, Object>> userList = employeeMapper.findEmployee(queryWrapper, page);
        // 获取分页信息
        return WebUtils.flushSuccess(userList);
    }


}
